package base62

import (
	"math"
	"strings"
)

var base62 string

func MustInit(bs string) {
	if len(bs) == 0 {
		panic("need base string!")
	}
	base62 = bs
}

func Int2String(seq uint64) string {
	bl := []byte{}
	if seq == 0 {
		return string(base62[0])
	}
	for seq > 0 {
		mod := seq % 62
		div := seq / 62
		bl = append(bl, base62[mod])
		seq = div
	}
	return string(reverse(bl))
}

func reverse(by []byte) []byte {
	for i, j := 0, len(by)-1; i < j; i, j = i+1, j-1 {
		by[i], by[j] = by[j], by[i]
	}
	return by
}
func String2Int(s string) (seq uint64) {
	bl := []byte(s)
	bl = reverse(bl)
	for idx, b := range bl {
		base := math.Pow(64, float64(idx))
		seq += uint64(strings.Index(base62, string(b))) * uint64(base)
	}
	return seq
}
